package com.akcome.hades.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;

import com.akcome.hades.domain.BmpIncome;
import com.akcome.hades.domain.enums.IncomeStatusEnum;
import com.akcome.hades.repository.BmpIncomeRepository;
import com.akcome.hades.service.BmpIncomeService;


/**
 * 收益记录Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class BmpIncomeServiceImpl implements BmpIncomeService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BmpIncomeRepository bmpIncomeRepository;
	
	@Override
	public BmpIncome save(BmpIncome bmpIncome){
		return bmpIncomeRepository.save(bmpIncome);
	}
	
	@Override
	public BmpIncome findOne(Long id){
		return bmpIncomeRepository.findOne(id);
	}
	
	@Override
	public List<BmpIncome> clearAuditData(){
		return bmpIncomeRepository.findAll(new Specification<BmpIncome>() {
			@Override
			public Predicate toPredicate(Root<BmpIncome> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> params = new ArrayList<Predicate>();
				params.add(criteriaBuilder.equal(root.get("status"), IncomeStatusEnum.INIT.getCode()));
				criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id")));
                Predicate[] predicates = new Predicate[params.size()];
                criteriaQuery.where(params.toArray(predicates));
                return null;
			}
		});
	}
	
	@Override
	public List<BmpIncome> findAll(){
		return bmpIncomeRepository.findAll();
	}
	
	@Override
	public Page<BmpIncome> findByPage(final Long accountId,final String payMode,final String coinType,final String status,final String miningDate, Pageable page){
		return bmpIncomeRepository.findAll(new Specification<BmpIncome>() {
			
			@Override
			public Predicate toPredicate(Root<BmpIncome> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> params = new ArrayList<Predicate>();
				if(null!=accountId){
					params.add(criteriaBuilder.equal(root.get("accountId"), accountId));
				}
				
				if(StringUtils.isNotBlank(coinType)){
					params.add(criteriaBuilder.equal(root.get("coinType"), coinType));
				}
				
				if(StringUtils.isNotBlank(payMode)){
					params.add(criteriaBuilder.equal(root.get("payMode"), payMode));
				}
				
				if(StringUtils.isNotBlank(status)){
					params.add(criteriaBuilder.equal(root.get("status"), status));
				}
				
				if(StringUtils.isNotBlank(miningDate)){
					try {
						params.add(criteriaBuilder.equal(root.get("miningDate"), new SimpleDateFormat("yyyy-MM-dd").parse(miningDate)));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				
				
                Predicate[] predicates = new Predicate[params.size()];
                criteriaQuery.where(params.toArray(predicates));
				
				return null;
			}
		}, page);
	}
	
	@Override
	public int cleatAudit(String miningDate,String payMode,String coinType,List ids){
		if(ListUtils.isEmpty(ids)){
			return bmpIncomeRepository.cleatAudit(miningDate, payMode, coinType);
		}else{
			return bmpIncomeRepository.cleatAudit(miningDate, payMode, coinType,ids);
		}
	}
}
